/*
 * Title: HotShop
 * Description: HotShop
 * Author: Alex Lee
 * Email: liyao20060101@gmail.com
 * Version: 2.0
 * Created on: 2010-9-15 上午10:55:07
 *
 * Copyright (c) 2011 www.vhotshop.com
 */

package com.frame.common.context;

import java.io.IOException;
import java.util.Map;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


/**
 * Spring 容器
 * @author alex.lee at 2010-9-15 上午10:57:35
 *
 * desc：
 */
public class SpringContainer implements ApplicationContextAware{
	
	public SpringContainer() {
	}

	private static ApplicationContext applicationContext;

	public void setApplicationContext(ApplicationContext arg0){
		applicationContext = arg0;
		
		try {
			// CommonConstants.PROJECT_PATH = applicationContext.getResource("/").getFile().getAbsolutePath();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据spring配置文件中的id获取对象
	 * @param id bean的id
	 * @return 对象
	 */
	public static Object getBean(String id) {
		return applicationContext.getBean(id);
	}
	
	@SuppressWarnings("unchecked")
	public static Object getBeanByClass(Class<? extends Object> clazz) {
		Map map = applicationContext.getBeansOfType(clazz);
		
		Set<String> keyset = map.keySet();
		for (String id : keyset) {
			return map.get(id);
		}
		return null;
	}
	
	public static int getBeanNumber(Class<? extends Object> clazz) {
		return applicationContext.getBeansOfType(clazz).size();
	}
}
